package jmine.tec.persist.jms;

import javax.jms.Message;
import javax.jms.MessageListener;

import jmine.tec.annotations.Unmodifiable;
import jmine.tec.persist.jms.domain.JMSStoredMessage;
import jmine.tec.utils.io.IOUtils;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import bancosys.tec.persist.controller.AbstractPersistenceEnabledController;

/**
 * @author takeshi
 */
@Unmodifiable
public class JMSStoreController extends AbstractPersistenceEnabledController implements ApplicationContextAware {

    private volatile ApplicationContext applicationContext;

    /**
     * {@inheritDoc}
     * 
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * @param message {@link JMSStoredMessage}
     */
    public void reprocess(JMSStoredMessage message) {
        MessageListener listener = (MessageListener) applicationContext.getBean(message.getListenerBeanName(), MessageListener.class);
        try {
            listener.onMessage((Message) IOUtils.deserializeObject(message.getMessage()));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}
